<!--
 * @Description：基础管理--系统角色管理
 * @author： wlj
 * @date： 2025/04/24 16:28:08
!-->
<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item label="角色名称">
        <el-input v-model="listQuery.name" placeholder="请输入角色名称,回车搜索" style="width: 300px; text-align: right;" @keyup.enter.native="handleResetPage">
          <el-button slot="append" icon="el-icon-search" @click="handleResetPage" />
        </el-input>
      </el-form-item>
      <el-button type="primary" @click="handleAddRole()">新增</el-button>
    </el-form>
    <el-table v-loading="listLoading" :data="tableList" border fitedit stripe>
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="角色名称" prop="name" />
      <el-table-column label="角色编码" prop="code" />
      <el-table-column label="角色类型" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type===1?'内置':scope.row.type===2?'自建':'' }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status===1?'启用':scope.row.status===0?'停用':'' }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="排序" prop="sort" />
      <el-table-column label="备注" prop="description" show-overflow-tooltip />
      <el-table-column width="100" label="操作" align="center">
        <template slot-scope="scope">
          <el-dropdown class="operation-style">
            <span class="el-dropdown-link">  操作<i class="el-icon-arrow-down el-icon--right"></i>  </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="scope.row.type===2" @click.native="handleEdit(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleAuthorize(scope.row)">授权</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.type===2" @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div v-if="count > 0">
      <pagination
        :total="count"
        layout="total,sizes, prev, pager, next, jumper"
        :page.sync="listQuery.pageSort.pageIndex"
        :limit.sync="listQuery.pageSort.pageSize"
        @pagination="search"
      />
    </div>
    <!-- 弹窗 -->
    <el-dialog
      v-loading.fullscreen.lock="fullscreenLoading"
      :title="dialogFormTitle"
      :visible.sync="dialogFormVisible"
      width="30%"
      destroy-on-close
      lock-scroll
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px" @submit.native.prevent>
        <el-form-item label="名称" prop="name">
          <el-input v-model.trim="form.name" :disabled="type === 'info'" @input="handleInput" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model.trim="form.code" :disabled="type === 'edit'" />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model.trim="form.description" type="textarea" :autosize="{ minRows: 2 }" maxlength="300" show-word-limit />
        </el-form-item>
        <!-- <el-form-item v-if="type === 'add'" label="状态">
          <el-radio-group v-model="form.status" v-removeAriaHidden :disabled="type === 'info'">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model.trim="form.sort" :min="0" :max="1000" label="数字越小，越靠前"></el-input-number>
          <span style="margin: 0 10px;"></span>
          <span style="font-size:12px">数字越小，越靠前</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancelDialog()">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 授权弹窗 -->
    <UserAuthorization
      v-if="dialogAuthorizedVisible"
      :role-code="roleCode"
      :role-name="roleName"
      :role-state="roleState"
      :platform="listQuery.platform"
      @cancel="CancelAuthuorize"
    ></UserAuthorization>
  </div>
</template>

<script>
import UserAuthorization from '@/components/UserAuthorization'
import { selectRole, addRole, alterRole, deleteRole } from '@/api/basic/sysRole'
export default {
  components: { UserAuthorization },
  data() {
    return {
      fullscreenLoading: false,
      // 当前点击授权的角色code
      roleCode: '',
      roleName: '',
      roleState: '',
      listLoading: false,
      listQuery: {
        pageSort: {
          pageIndex: 1,
          pageSize: 20
        },
        status: undefined,
        name: '',
        platform: 'ADMIN'
      },
      count: 0,
      tableList: [],
      type: '',
      dialogFormTitle: '',
      dialogFormVisible: false,
      form: { // 新增和编辑数据存放
        id: undefined,
        code: '',
        name: '',
        status: 1,
        sort: 0,
        description: '',
        platform: 'ADMIN'
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入角色编码', trigger: 'blur' }]
      },
      dialogAuthorizedVisible: false
    }
  },
  created() {
    this.search()
  },
  methods: {
    search() {
      this.listLoading = true
      selectRole(this.listQuery).then(res => {
        if (res.success) {
          this.tableList = res.data.rows
          this.count = res.data.total
          this.listLoading = false
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 点击新增
    handleAddRole() {
      this.type = 'add'
      this.dialogFormTitle = '新增'
      this.dialogFormVisible = true
    },
    // 新增--》名称输入框事件
    handleInput() {
      if (this.type === 'add') {
        this.form.name = this.inputPInYinCode(this.form.name).name
        this.form.code = this.inputPInYinCode(this.form.name).code
      }
    },
    // 点击编辑
    handleEdit(row) {
      this.form = {
        id: row.id,
        code: row.code,
        name: row.name,
        status: row.status,
        sort: row.sort,
        description: row.description,
        platform: row.platform
      }
      this.dialogFormTitle = '编辑'
      this.type = 'edit'
      this.dialogFormVisible = true
    },
    // 打开--授权--弹窗
    handleAuthorize(row) {
      this.roleCode = row.code
      this.roleName = row.name
      this.roleState = parseInt(row.status)
      this.dialogAuthorizedVisible = true
    },
    // 关闭--授权弹窗
    CancelAuthuorize() {
      this.dialogAuthorizedVisible = false
    },
    // 删除角色 （over）
    handleDelete(row) { // 单个删除
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDelete([row.code])
      }).catch(() => {})
    },
    // 执行删除操作
    doDelete(ids) {
      deleteRole(ids).then(res => {
        if (res.success) {
          this.$message({ message: '删除成功', type: 'info' })
          this.search()
        } else this.$message.error(res.message)
      })
    },
    // 点击确定新增/编辑
    handleConfirm(formRef) {
      this.$refs[formRef].validate(v => {
        if (v) {
          this.fullscreenLoading = true
          if (this.type === 'edit') { // 编辑
            const editData = {
              name: this.form.name,
              code: this.form.code,
              description: this.form.description,
              sort: this.form.sort
            }
            alterRole(editData).then(res => {
              this.$message({ message: res.message || '编辑成功', type: 'success' })
              this.search()
              this.handleCancelDialog()
              this.fullscreenLoading = false
            }).catch(() => {
              this.fullscreenLoading = false
            })
            // 新增
          } else if (this.type === 'add') {
            addRole(this.form).then(res => {
              if (res.success) {
                this.$message({ message: res.message || '新增成功', type: 'success' })
                this.search()
                this.handleCancelDialog()
                this.fullscreenLoading = false
              }
            }).catch(() => {
              this.fullscreenLoading = false
            })
          } else {
            this.fullscreenLoading = false
            this.handleCancelDialog()
          }
        }
      })
    },
    // 对话框的取消显示
    handleCancelDialog() {
      this.dialogFormVisible = false
      this.form = {
        id: undefined,
        code: '',
        name: '',
        status: 1,
        sort: 0,
        description: '',
        platform: 'ADMIN'
      }
    },
    // 切换搜索条件时，页码重置
    handleResetPage() {
      this.listQuery.pageSort.pageIndex = 1
      this.search()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

